/*
 * ValidationScope.h
 *
 *  Created on: 01/06/2013
 *      Author: Jeffrey Alvarez Masson
 */

#ifndef VALIDATIONSCOPE_H_
#define VALIDATIONSCOPE_H_

#include "ValidationRule.h"
#include <vector>
#include <map>
#include <string>
#include <boost/shared_ptr.hpp>

namespace ValidationRules {

/**
 * Represents a ValidationRule shared pointer.
 */
typedef boost::shared_ptr<ValidationRule> ValidationRuleSPtr;

/**
 * Represents the map to store validation rules.
 */
typedef std::map<std::string, ValidationRuleSPtr> RuleMap;

/**
 * Represents a scope for validation rules.
 */
class ValidationScope {
private:
	RuleMap rules;
	static std::vector<ValidationScope*> allScopes;
	static ValidationScope* globalScope;

public:
	/**
	 * Create a new instance of the ValidationScope class. Sets the current scope as the instance of the class.
	 */
	ValidationScope() { allScopes.push_back(this); }
	/**
	 * Deallocates the instance's  resources. Sets the current scope to the previous last.
	 */
	virtual ~ValidationScope() { allScopes.pop_back(); }
	/**
	 * Gets the rule map.
	 * @return Validation rule map.
	 */
	RuleMap* GetRules() { return &rules; }
	/**
	 * Gets a specific rule.
	 * @param rule Rule name.
	 * @return Validation rule with the provided name. NULL if the rule does not exist.
	 */
	ValidationRule* GetRule(std::string rule) { return rules.find(rule) != rules.end() ? rules[rule].get() : NULL; }
	/**
	 * Adds a validation rule to the map.
	 * @param rule Rule to add.
	 */
	void AddRule(ValidationRuleSPtr rule) { rules[rule->GetRuleName()] = rule; }
	/**
	 * Gets the current validation scope (last instance of ValidationScope created).
	 * @return Current validation scope instance.
	 */
	static ValidationScope* GetCurrent() { return allScopes[allScopes.size() - 1]; }
};

} /* namespace ValidationRules */
#endif /* VALIDATIONSCOPE_H_ */
